using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace cksum
{
    public class Program
    {
        public static int Main(string[] args)
        {
            if (args == null || args.Length < 0)
            {
                Console.WriteLine("Usage: cksum FILE");
                return 1;
            }

            try
            {
                foreach (FileInfo fi in GetFiles(args))
                {
                    Console.WriteLine("{0} {1} {2}", PublicDomain.Cksum.Compute(fi), fi.Length, fi.FullName);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace);
            }

            return 0;
        }

        private static List<FileInfo> GetFiles(string[] args)
        {
            List<FileInfo> result = new List<FileInfo>();
            string arg;

            for (int i = 0; i < args.Length; i++)
            {
                arg = args[i];

                if (arg.IndexOf('*') == -1)
                {
                    result.Add(new FileInfo(arg));
                }
                else
                {
                    DirectoryInfo dir;
                    int pathIndex;

                    if ((pathIndex = arg.LastIndexOfAny(new char[] { Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar })) == -1)
                    {
                        dir = new DirectoryInfo(Environment.CurrentDirectory);
                    }
                    else
                    {
                        dir = new DirectoryInfo(arg.Substring(0, pathIndex));
                        arg = arg.Substring(pathIndex + 1);
                    }
                    result.AddRange(dir.GetFiles(arg));
                }
            }
            return result;
        }
    }
}
